package indi.jp.project.business.yunying.service.impl;

import indi.jp.project.business.pub.dao.ProjectDAO;
import indi.jp.project.business.pub.pojo.ProjectStatusEnum;
import indi.jp.project.business.pub.pojo.po.ProjectPO;
import indi.jp.project.business.yunying.dao.ProjectFiledDAO;
import indi.jp.project.core.entity.Datagrid;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.entity.po.AttachmentPO;
import indi.jp.project.core.service.AttachmentService;
import indi.jp.project.core.util.SerialUtil;
import indi.jp.project.process.entity.FlowTaskNotifyDTO;
import indi.jp.project.process.service.FlowTaskService;
import indi.jp.project.business.yunying.entity.ProjectFiled;
import indi.jp.project.business.yunying.service.ProjectFiledService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.*;

@Service("projectInitiateService")
public class ProjectFiledServiceImpl implements ProjectFiledService {

    @Resource
    private ProjectFiledDAO projectFiledDAO;

    @Resource
    private SerialUtil serialUtil;

    @Resource
    private FlowTaskService flowTaskService;

    @Resource
    private AttachmentService attachmentService;

    @Resource
    private ProjectDAO projectDAO;

    @Override
    public JsonResult queryGrid(ProjectFiled projectFiled) throws SQLException {
        List<ProjectFiled> list = projectFiledDAO.queryList(projectFiled);
        int count = projectFiledDAO.queryCount(projectFiled);
        Datagrid<ProjectFiled> datagrid = new Datagrid<>(list, count);
        return JsonResult.success(datagrid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult save(ProjectFiled projectFiled) throws Exception {
        String id;
        if (StringUtils.isBlank(projectFiled.getId())) {
            id = String.valueOf(serialUtil.nextId());
            projectFiled.setId(id);
            projectFiledDAO.insert(projectFiled);
        } else {
            id = projectFiled.getId();
            projectFiledDAO.update(projectFiled);
        }
        List<AttachmentPO> attachments = projectFiled.getAttachments();
        if (attachments != null && attachments.size() > 0) {
            attachmentService.delete(id);
            attachmentService.save(attachments, id);
        }
        flowTaskService.saveTaskFlow(id, projectFiled.getFlowList());
        return JsonResult.success("保存成功");
    }

    @Override
    public JsonResult findById(String id) throws Exception {
        ProjectFiled projectFiled = projectFiledDAO.findById(id);
        List<AttachmentPO> attachments = attachmentService.queryAttachments(id, null);
        projectFiled.setAttachments(attachments);
        return JsonResult.success(projectFiled);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult deleteById(String id) throws Exception {
        projectFiledDAO.deleteById(id);
        flowTaskService.deleteFlowByTaskId(id);
        attachmentService.delete(id);
        return JsonResult.success();
    }

    @Override
    public void notify(FlowTaskNotifyDTO flowTaskNotifyDTO) throws Exception {
        if("submit".equals(flowTaskNotifyDTO.getOperation())){
            ProjectFiled projectFiled = projectFiledDAO.findById(flowTaskNotifyDTO.getTaskId());
            ProjectPO projectPO = new ProjectPO();
            projectPO.setId(projectFiled.getProjectId());
            projectPO.setStatus(ProjectStatusEnum.IN_PA.getCode());
            projectPO.setStatusName(ProjectStatusEnum.IN_PA.getName());
            projectDAO.update(projectPO);
        }
        if (flowTaskNotifyDTO.getNextState() == 99) {
            ProjectFiled projectFiled = projectFiledDAO.findById(flowTaskNotifyDTO.getTaskId());
            ProjectPO projectPO = new ProjectPO();
            BeanUtils.copyProperties(projectFiled, projectPO);
            projectPO.setId(projectFiled.getProjectId());
            projectPO.setCode(projectFiled.getProjectCode());
            projectPO.setName(projectFiled.getProjectName());
            projectPO.setAddrCode(projectFiled.getProjectAddrCode());
            projectPO.setAddrName(projectFiled.getProjectAddrName());
            projectPO.setAddrSpec(projectFiled.getProjectAddrSpec());
            projectPO.setStatus(ProjectStatusEnum.HAS_PA.getCode());
            projectPO.setStatusName(ProjectStatusEnum.HAS_PA.getName());
            projectDAO.update(projectPO);
        }
    }
}
